package DALFacades;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.List;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.rmi.PortableRemoteObject;

import Backwards.EJBBackward;
import DomainLayer.DomainEntityFactory;
import DomainLayer.MessageEntity;
import DomainLayer.UserEntity;
import DomainLayer.ViewEntity;
import Helpers.ApplicationLogger;

import com.ejbBeans.NodeHomeRemote;
import com.ejbBeans.NodeRemote;
import com.ejbBeans.UserHomeRemote;
import com.ejbBeans.UserRemote;


public class ConcreteFacadeServer1 implements IFacade {

	private DomainEntityFactory<MessageEntity> messageFactory = new DomainEntityFactory<MessageEntity>();
	private DomainEntityFactory<ViewEntity> viewFactory = new DomainEntityFactory<ViewEntity>();
	private DomainEntityFactory<UserEntity> userFactory = new DomainEntityFactory<UserEntity>();
	
	public Object GetLastNView() {
		List<ViewEntity> list = (List<ViewEntity>) viewFactory.getEntityCollectionInstance();
		return list;
	}

	public List<MessageEntity> GetMessagesByUserId(String userIdString) {
		ApplicationLogger.getInstance().LOG.info("call method GetMessagesByUserId in FacadeServer");

		EJBBackward ejbB = new EJBBackward();
		EJBBackward.BeanController messagesController = ejbB.new BeanController();
		Object bean = messagesController.GetBeanByJNDILookup("java:global/TreeEJBBean/Node!com.ejbBeans.NodeHomeRemote");
		
		ApplicationLogger.getInstance().LOG.info("UserID: " + userIdString.toString());
		
		List<MessageEntity> result = (List<MessageEntity>) messageFactory.getEntityCollectionInstance();
		NodeHomeRemote nhr = (NodeHomeRemote)PortableRemoteObject.narrow(bean, NodeHomeRemote.class);
		Collection<NodeRemote> messages = null;
		try {
			messages = nhr.findByUserId(userIdString);
			ApplicationLogger.getInstance().LOG.info("Bean created");
		} catch (RemoteException e) {
			ApplicationLogger.getInstance().LOG.error("ERROR: " + e.getMessage());
		} catch (FinderException e) {
			ApplicationLogger.getInstance().LOG.error("ERROR: " + e.getMessage());
		}
		try {
			ApplicationLogger.getInstance().LOG.info("Get messages");
			
			for (NodeRemote message : messages) {
				MessageEntity newMessageEntity = messageFactory.getEntityInstance(MessageEntity.class);
				newMessageEntity.setId(message.getId());
				newMessageEntity.setMessageString(message.getValue());
				result.add(newMessageEntity);
			}

			ApplicationLogger.getInstance().LOG.info("Messages list has inited");
		} catch (RemoteException e) {
			ApplicationLogger.getInstance().LOG.error("ERROR: " + e.getMessage());
		}
		
		ApplicationLogger.getInstance().LOG.info("RESULT: " + result);
		return result;
	}

	public Object GetImageById(String keyString) throws Throwable {
		// TODO Auto-generated method stub
		return null;
	}

	public Object LoadImage(String imageNameString, Object imageByteArray)
			throws Throwable {
		// TODO Auto-generated method stub
		return null;
	}

	public void DeleteImage(String imageNameString) throws Throwable {
		// TODO Auto-generated method stub
		
	}

	public void AddNotification(String contentString) throws Throwable {
		// TODO Auto-generated method stub
		
	}

	public UserEntity GetUserEntityByLoginAndPassword(String login, String password) throws Throwable {
		EJBBackward ejbB = new EJBBackward();
		EJBBackward.BeanController userController = ejbB.new BeanController();
		
		Object bean = userController.GetBeanByJNDILookup("java:global/TreeEJBBean/User!com.ejbBeans.UserHomeRemote");
		
		ApplicationLogger.getInstance().LOG.info("Login: " + login + " Password: " + password);
		
		UserEntity result = (UserEntity) userFactory.getEntityInstance(UserEntity.class);
		UserHomeRemote uhr = (UserHomeRemote)PortableRemoteObject.narrow(bean, UserHomeRemote.class);
		UserRemote beanImpl = null;
		
		try {
			beanImpl = uhr.findByLoginPassword(login, password);
			ApplicationLogger.getInstance().LOG.info("Bean created");
		} catch (RemoteException e) {
			ApplicationLogger.getInstance().LOG.error("ERROR: " + e.getMessage());
		}

		try {
			ApplicationLogger.getInstance().LOG.info("getUser");

			UserEntity newUserEntity = userFactory.getEntityInstance(UserEntity.class);
			newUserEntity.setId(beanImpl.getId());
			
			result = newUserEntity;

			ApplicationLogger.getInstance().LOG.info("user has inited");
		} catch (RemoteException e) {
			ApplicationLogger.getInstance().LOG.error("ERROR: " + e.getMessage());
		}
		
		return result;
	}

//	@Override
//	public Boolean AddMessage(String message, String userIdForm, String userIdTo, boolean isPrivate) throws Throwable {
//		
//		ApplicationLogger.getInstance().LOG.info("call method AddMessage in FacadeServer");
//
//		EJBBackward ejbB = new EJBBackward();
//		EJBBackward.BeanController messagesController = ejbB.new BeanController();
//		Object bean = messagesController.GetBeanByJNDILookup("java:global/TreeEJBBean/Node!com.ejbBeans.NodeHomeRemote");
//		
//		NodeHomeRemote nhr = (NodeHomeRemote)PortableRemoteObject.narrow(bean, NodeHomeRemote.class);
//		try {
//			NodeRemote node = nhr.create(1);
//			node.setValue(message);
//			node.setUserId(userIdTo);
//			ApplicationLogger.getInstance().LOG.info("Bean created");
//		} catch (RemoteException e) {
//			ApplicationLogger.getInstance().LOG.error("ERROR: " + e.getMessage());
//		} catch (CreateException e) {
//			ApplicationLogger.getInstance().LOG.error("ERROR: " + e.getMessage());
//		}
//		
//		return true;
//	}

}
